// pages/company_search/company_search.js
const app = getApp();
const $get_api = require('../../common/api').HTTP;
Page({

    /**
     * 页面的初始数据
     */
    data: {
      keyword:'', //上一页传过来的关键词
      cate_id:null, //分类
      data_title:"",
      keyword:'',// 搜索框内容，使用的是简易的双向绑定 //温习一下：第一层才能进行双向绑定
      keywords:[],//历史搜索的数组
      label_img:app.imgUrl + "my_qiye/label_img.png"+ app.version,
      right_icon:app.imgUrl + "youjiantou.png"+ app.version,
      img:app.imgUrl + "conpany_search/company_icon.png"+ app.version,
      nomore:app.imgUrl + "no_more.png"+ app.version,
      tabList: {
        "top":[],
        "bottom":[],
        },
        isLoginBtn:true,
        no_more: false, 
        isLoading: true, 
        list_page: 1, 
        last_page:1,
        limit:5
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      console.log(options,"options");
      let that = this;
      that.data.cate_id = options.cate_id;
      that.data.keyword = options.keyword;
      that.data.data_title = options.data_title;
      that.setData({
        keyword: that.data.keyword 
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      let that = this
      wx.setNavigationBarTitle({
        title: that.data.data_title
      })
      that.shopList(1,that.data.limit);
      
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
      let _this = this;
      // 构建页面参数
      let params = app.getShareUrlParams({});  //app.js分享为封装
      return {
          title: app.useConst.SET_INFO.share_obj.title,
          //   path: "/pages/order_list/order_list?" + params,
          path: "/pages/index/index?" + params,
          imageUrl: app.useConst.SET_INFO.share_obj.imageUrl
      };
    },
    //点击搜索按钮
    onSearch(){
      let that = this
      // this.data.keyword获取数据，
      //trim去除字符串前后多余空格
      const keyword=this.data.keyword.trim()
      that.data.keyword = keyword
     //增加数组长度的步骤也可以使用展开运算符
     const arr=[keyword,...this.data.keywords]
     
      this.setData({
          //先转化为set去重
          //然后重新转化为数组
          keywords:Array.from(new Set(arr)),
          //搜索完成清空搜索框
          // keyword:''
      })
      that.shopList(1,that.data.limit);
      console.log(this.data.keyword)
  },
  //清空历史记录
  onClear(){
    //清空历史记录数组
    this.setData({
        keywords:[]
    })
  },
  shopList:function(page = 1, limit = 20){
    let that = this;
    //select_type 1最新 2销量
    console.log(that.data.keyword);
    $get_api.getEnterpriseLits({ 
        page:page,
        limit:limit,
        keyword:that.data.keyword,
        cate_id:that.data.cate_id,
    }).then(r => { 
    let data = r.data.data;
    console.log(data,"获取列表成功")
    that.data.page_count = data.count;
    that.data.tabList.bottom = [];
    that.data.tabList.bottom = data.list;
    that.data.last_page = Math.ceil(data.count / that.data.limit);
    console.log(data.count,"arr",that.data.limit);
    this.setData({
        "tabList.bottom":that.data.tabList.bottom,
        page_count:that.data.page_count
    })
  })
 },
 // 下拉刷新方法
 bindDownLoad() {
  let that = this;
  console.log("123sdfsdf");
  console.log(that.data.list_page,"that.data.list_page",that.data.last_page);
  if (that.data.list_page >= that.data.last_page) {
  that.setData({
      no_more: true
  });
    return false;
  }
  that.getGoodsList(true, ++that.data.list_page,that.data.limit);
  },
  getGoodsList(isPage, list_page, limit = 20) {
    let _this = this;
    $get_api.getEnterpriseLits({ 
        page: list_page || 1,
        limit:limit,
    }).then(r => { 
        let resList = r.data.data;
        let dataList = _this.data.tabList.bottom;
        let arr = resList.list;
        
        console.log(resList,"arr");
       
        if (isPage == true) {
        _this.setData({
            'tabList.bottom': dataList.concat(arr),
            isLoading: false,
        });
        console.log(_this.data.tabList.bottom,"menuArr.shops");
        } else {
        _this.setData({
            'tabList.bottom': resList.list,
            isLoading: false,
            
        });
        }
        if (_this.data.list_page >= _this.data.last_page) {
        this.setData({
            no_more: true
        });
        }
    })
  },
 goto_xiangqing_page:function(e){
  let id = e.currentTarget.dataset.upage;
  let title = e.currentTarget.dataset.title;
  wx.navigateTo({
    url: '../company_introduce/company_introduce?id='+id+'&title='+title,
  })
 }
})